#include <stdio.h>
#include <stdlib.h>
#include "bt.h"

Status InitBiTree(BTree *t) {
    *t = NULL;
    return OK;
}

#if COMMENT
Status CreateBiTree_Pre(BTree *B) {
    int c;
    if ((c = getchar()) == 'Z') {
        *B = NULL;
        return OK;
    }
    *B = (TNode *)malloc(sizeof(TNode));
    (*B)->data = c;
    CreateBiTree_Pre(&((*B)->left));
    CreateBiTree_Pre(&((*B)->right));
    return OK;
}
#endif

BTree PreCreateBiTree() {
    int c;
    if ((c = getchar()) == 'Z') {
        return NULL;
    }
    BTree p;
    p = (TNode *)malloc(sizeof(TNode));
    p->data = c;
    p->left = PreCreateBiTree();
    p->right = PreCreateBiTree();
    return p;
}

void PreTraverse(const BTree t) {
    if (t == NULL) {
        return;
    }
    printf ("%c", t->data);
    PreTraverse(t->left);
    PreTraverse(t->right);
    return;
}

void InTraverse(const BTree t) {
    if (t == NULL) {
        return;
    }
    InTraverse(t->left);
    printf ("%c", t->data);
    InTraverse(t->right);
    return;
}

void PostTraverse(const BTree t) {
    if (t == NULL) {
        return;
    }
    PostTraverse(t->left);
    PostTraverse(t->right);
    printf ("%c", t->data);
    return;
}

TBTree InThrBiTree(BTree t) {
    TBTree tbt;
    tbt = CopyBiTree(t);
    return tbt;
}

TBTree CopyBiTree(BTree t)
{
    if (t == NULL) {
        return NULL;
    }
    TBTree tbt;
    tbt = (ThrTNode *)malloc(sizeof(ThrTNode));
    tbt->data = t->data;
    tbt->left = CopyBiTree(t->left);
    tbt->right = CopyBiTree(t->right);
    return tbt;
}
